package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.CartItem;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.service.CartService;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class AddItemToCartServlet extends HttpServlet {
    //Servlet的功能即负责中转
    //1.处理完请求后的跳转页面
    private static final String VIEW_CART = "/jsp/cart/Cart.jsp";
    private static final String ERROR = "/jsp/common/Error.jsp";
    private CatalogService catalogService;
    private CartService cartService;
    //2.定义处理该请求所需要的数据
    private String workingItemId;
    private Cart cart;             //购物车
    //private String workingItemId;

    private String itemIdString;
    private CartItem cartItem;
    private int localQuatity;
    //3.是否需要调用业务逻辑层


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       HttpSession session = request.getSession();
        if (session.getAttribute("account") == null) {
            session.setAttribute("message", "Please Sign In At First!");
            request.getRequestDispatcher(ERROR).forward(request, response);

        } else
        {
            cartService = new CartService();
            workingItemId = request.getParameter("workingItemId");

            Account account;
            //从对话中，获取购物车

            cart = (Cart) session.getAttribute("cart");
            account = (Account) session.getAttribute("account");

            if (cart == null) {
                //第一次使用购物车
                cart = new Cart();
            }

            if (cart.containsItemId(workingItemId)) {
                //已有该物品，数量加一
                cart.incrementQuantityByItemId(workingItemId);
                cartService.incrementQuantityByItemId(account.getUsername(),workingItemId);
                if (account != null) {
                    String strBackUrl = "https://" + request.getServerName() + ":" + request.getServerPort()
                            + ((HttpServletRequest) request).getContextPath() + request.getServletPath() + "?" + (request.getQueryString());

                    LogService logService = new LogService();
                    Item item = catalogService.getItem(workingItemId);
                    String logInfo = logService.logInfo(" ") + strBackUrl + " " + item + "数量+1 ";
                    logService.insertLogInfo(account.getUsername(), logInfo);
                }
            } else {
                catalogService = new CatalogService();
                boolean isInStock = catalogService.isItemInStock(workingItemId);
                Item item = catalogService.getItem(workingItemId);
                cart.addItem(item, isInStock);
                cartService.addItem(account.getUsername(),workingItemId);
                session.setAttribute("cart", cart);

                if (account != null) {
                    String strBackUrl = "https://" + request.getServerName() + ":" + request.getServerPort()
                            + request.getContextPath() + request.getServletPath() + "?" + (request.getQueryString());

                    LogService logService = new LogService();
                    String logInfo = logService.logInfo(" ") + strBackUrl + " add product " + item + " to cart";
                    logService.insertLogInfo(account.getUsername(), logInfo);
                }


                request.getRequestDispatcher(VIEW_CART).forward(request, response);
            }
        }
    }
}
